<?php
namespace Home\Controller;
/*use Think\Controller;*/
class IndexController extends BaseController {
    public function index(){
      if($_GET['friend']||$_GET['user_id']){
      //进入好友空间
       $user_id = $_GET['user_id'];
      }else{
    	//最新
      $user_id = $_SESSION['users']['user_id'];
     }
    $list = D('Img')->order('createtime desc')->where('zutu_id < 0 and user_id ='.$user_id)->limit(0,4)->select();
      $this->assign('list',$list);
      //组图
      $zutu =D('Zutu')->order('createtime desc')->where('user_id = '.$user_id)->limit(0,3)->select();
      foreach ($zutu as $key => $value) {
        $zutulist = D('Img')->where('zutu_id = '.$value['zutu_id'])->field('thumb_url')->order('sort asc')->find();
        $value['thumb_url'] =$zutulist['thumb_url'];
        $zutu[$key]=$value;
      }
      $this->assign('zutu',$zutu);

        //图片内容类型

        $ctypelist = D('Ctype')->where('is_open=1 and user_id='.$user_id)->order('sort desc')->select();
        foreach ($ctypelist as $key => $value) {
            $list = D('Img')->where('ctype_id = '.$value['ctype_id'])->limit(0,6)->order('sort asc')->select();
            $value['list'] = $list;
            $ctypelist[$key] = $value;
          if(empty($ctypelist[$key]['list'])){
            unset($ctypelist[$key]);
          }
        }
     $this->assign('ctypelist',$ctypelist);
     $this->display('index');
  }
   public function getajax(){
      
   }

//点击更多显示内容
   public function img_list(){
    extract($_GET);
    //查询最新的20条记录
      $page_size = 3;
      $page_now = empty($page_now)? 1:$page_now;
    if($title == 'news'){
      $count = D('Img')->where('zutu_id < 0')->count();
      $page_count = ceil($count/$page_size);
      if($page_now>$page_count){
        echo 0;
        exit;
      }
      $page_start= ($page_now-1)*$page_size;
      $limit = "$page_start,$page_size";
      $list = D('Img')->where('zutu_id < 0')->order('createtime desc')->limit($limit)->select();
      $title = "news";
      $name = "最新图片";
    }else if($title == 'zutu'){
      //组图（每个组图显示的是组图下面排序最大的第一张图片）
           $count = D('Zutu')->order('createtime desc')->count();
            $page_count = ceil($count/$page_size);
            if($page_now>$page_count){
              echo 0;
              exit;
            }
          $page_start= ($page_now-1)*$page_size;
          $limit = "$page_start,$page_size";
          $list =D('Zutu')->order('createtime desc')->limit($limit)->select();

            foreach ($list as $key => $value) {
              $zutulist = D('Img')->where('zutu_id = '.$value['zutu_id'])->field('thumb_url')->order('sort asc')->find();
              $value['thumb_url'] =$zutulist['thumb_url'];
              $list[$key]=$value;
            }
      $title = "zutu";
      $name="组图图片";
    }else{
        $count = D('Img')->where("ctype_id=$title")->count();
          $page_count = ceil($count/$page_size);
          if($page_now>$page_count){
            echo 0;
            exit;
          }
          $page_start= ($page_now-1)*$page_size;
          $limit = "$page_start,$page_size";
          $list = D('Img')->where("ctype_id=$title")->order('createtime desc')->limit($limit)->select();
          $title= $title;
          $name = $ctype_name;
    }
    if(!$list){
     $this->error('没有改组图片');
    }
    if(IS_AJAX){
      $this->ajaxReturn($list);
    }
    //判断时候还有数据或者数据小于pagesize的条数时，将添加的图片换成没有数据的图片
    if(count($list)<$page_size){
      $this->assign('nodata',"nodata");
    }
    $this->assign('name',$name);
    $this->assign('title',$title);
    $this->assign('list',$list);
    $this->display();
   }



   //显示组图的图片
   public function zutu_list(){


   }
}